from django.contrib.auth.hashers import check_password
from rest_framework import serializers

from mainapp.models import *
from common.error import NewsException


class TUserSerializer(serializers.ModelSerializer):  # 负责序列化用户模型
    class Meta:
        model = TUser
        fields = "__all__"


class CheckRegUserSerializer(serializers.Serializer):  # 用于验证用户提交的数据
    # 编写格式验证规则
    email = serializers.EmailField(required=True)  # 类属性对应于客户端提交的参数名
    password = serializers.CharField(max_length=16, required=True)
    password_ = serializers.CharField(max_length=16, required=True)
    vaild_code = serializers.CharField(max_length=6, required=True)

    def validate(self, attrs):  # 编写逻辑验证规则
        email = attrs.get("email")
        password = attrs.get("password")
        password_ = attrs.get("password_")
        vaild_code = attrs.get("vaild_code")
        if TUser.objects.filter(email=email).exists():
            raise NewsException("该账号已存在，不能注册！")
        if password != password_:
            raise NewsException("两次密码不一致！")
        if vaild_code:
            pass

        return attrs


class ProjectSerializer(serializers.ModelSerializer):  # 负责序列化项目模型
    class Meta:
        model = Project
        fields = "__all__"


